//
//  SQCodeViewController.m
//  energy
//
//  Created by 周曼迪 on 2021/7/6.
//

#import "SQCodeViewController.h"

#import "SQCodeModel.h"
#import "SQExaminationRecordModel.h"
#import "SQRiskPointModel.h"

#import "LCImageChoose.h"
#import "SQShowImage.h"
#import "AppDelegate+Tool.h"

#import "SQCodeReportViewController.h"


@interface SQCodeViewController ()<BaseTableVCInterface>
@property (weak, nonatomic) IBOutlet UITextField *tf_area;
@property (weak, nonatomic) IBOutlet UITextField *tf_name;
@property (strong, nonatomic) SQCodeModel *codeModel;

@property (weak, nonatomic) IBOutlet UIImageView *img_picOne;
@property (weak, nonatomic) IBOutlet UIImageView *img_picOneDel;
@property (weak, nonatomic) IBOutlet UIImageView *img_picTwo;
@property (weak, nonatomic) IBOutlet UIImageView *img_picTwoDel;
@property (weak, nonatomic) IBOutlet UIImageView *img_picThree;
@property (weak, nonatomic) IBOutlet UIImageView *img_picThreeDel;

@property (strong, nonatomic) NSString *hd_report_pic_one;
@property (strong, nonatomic) NSString *hd_report_pic_two;
@property (strong, nonatomic) NSString *hd_report_pic_three;

@property (copy, nonatomic) NSString *ir_risktype;
@property (copy, nonatomic) NSString *ir_mode;
@property (copy, nonatomic) NSString *ir_hidden_trouble;
@property (weak, nonatomic) IBOutlet UIView *view_pic;
@property (strong, nonatomic) IBOutlet UIView *contentView;

@property (weak, nonatomic) IBOutlet UILabel *lab_rp_name;
@property (weak, nonatomic) IBOutlet UILabel *lab_harmful_consequences_text;
@property (weak, nonatomic) IBOutlet UILabel *lab_rp_risk_content;
@property (weak, nonatomic) IBOutlet UILabel *lab_lec_level;
@property (weak, nonatomic) IBOutlet UILabel *lab_artificial_control;
@property (weak, nonatomic) IBOutlet UILabel *lab_management_control;
@property (weak, nonatomic) IBOutlet UILabel *lab_control_department;
@property (weak, nonatomic) IBOutlet UILabel *lab_responsible_post;

@property (weak, nonatomic) IBOutlet UILabel *lab_technology_control;
@property (weak, nonatomic) IBOutlet UILabel *lab_control_level_text;

@property (weak, nonatomic) IBOutlet UILabel *lab_training_measures;

@property (weak, nonatomic) IBOutlet UILabel *lab_risk_factor;

@end

@implementation SQCodeViewController

- (void)viewDidLoad {
    self.tableViewInterface = self;
    [super viewDidLoad];
    
    self.hd_report_pic_one = @"";
    self.hd_report_pic_two = @"";
    self.hd_report_pic_three = @"";
    
    if ([self.type isEqualToString:@"1"]) {
        self.navigationItem.title = @"扫码巡检";
        self.ir_risktype = @"0";
        self.ir_mode = @"1";
        [self getCodeData];
    }else if ([self.type isEqualToString:@"2"]){
        self.navigationItem.title = @"作业检查";
        self.ir_risktype = @"0";
        self.ir_mode = @"1";
        [self getCodeData];
    }
    self.ir_hidden_trouble = @"0";

    LC_WEAK_SELF
    self.cellTitle = ^(int Section, int Index) {
        SQCodeModelData * dataModel = weakSelf.codeModel.data[Section];
        SQCodeModelRiskPoint * dataPoint = dataModel.risk_point[Index];
        SQCodeReportViewController * codeReportVC = SQCodeReportViewController.new;
        codeReportVC.hd_ipm = weakSelf.ipm_id;
        codeReportVC.hd_ipm_text = weakSelf.ipm_id_text;
        codeReportVC.hd_rt = dataModel.rt_id;
        codeReportVC.hd_rt_text = dataModel.rt_name;
        codeReportVC.hd_rp_text = dataPoint.rp_name;
        codeReportVC.hd_rp = dataPoint.rp_id;
        codeReportVC.ipm_unit_one = weakSelf.codeModel.ipm_unit_one;
        codeReportVC.ipm_unit_two = weakSelf.codeModel.ipm_unit_two;
        codeReportVC.ipm_unit_three = weakSelf.codeModel.ipm_unit_three;
        codeReportVC.strType = @"4";
        codeReportVC.codeReportBack = ^(id  _Nonnull str) {
            dataPoint.rp_count = @"1";
            weakSelf.ir_hidden_trouble = @"1";
            
            [weakSelf reloadTableView];
        };
        [weakSelf pushVC:codeReportVC];
        
    };
   
    
    
}

-(void)getCodeData{
    [SQCodeModel getDataWithParams:@{@"ipm_id":self.ipm_id} WithCallBack:^(SQCodeModel * data) {
        self.codeModel = data;
        self.tf_area.text = [NSString stringWithFormat:@"%@/%@/%@",data.ipm_unit_one,data.ipm_unit_two,data.ipm_unit_three];
        
        self.tf_name.text = data.ipm_name;
    
        for (SQCodeModelData * dataModle  in data.data) {
            for (SQCodeModelRiskPoint *riskPoinModel  in dataModle.risk_point) {
                riskPoinModel.type = self.type;
               if ([self.type isEqualToString:@"2"]){
                   riskPoinModel.rp_count = @"0";
                }
            }
        }
        
        self.codeModel = data;
        [self reloadTableView];
    }];
}


-(NSInteger)sectionCount{
    return self.codeModel.data.count;
}

-(NSInteger)rowsCount:(NSInteger)section{
    SQCodeModelData * dataModel = self.codeModel.data[section];
    return dataModel.risk_point.count;
}


-(BaseModel *)modelForCell:(NSIndexPath *)indexPath{
    SQCodeModelData * dataModel = self.codeModel.data[indexPath.section];
    return dataModel.risk_point[indexPath.row];
}



- (void)tableViewSelectAtIndexPath:(NSIndexPath *)indexPath{
    
    SQCodeModelData * dataModel = self.codeModel.data[indexPath.section];
    SQCodeModelRiskPoint * pointModel = dataModel.risk_point[indexPath.row];
    [SQRiskPointModel getDataWithParams:@{@"rp_id":pointModel.rp_id} WithCallBack:^(SQRiskPointModel * data) {
        self.lab_rp_name.text = data.data.rp_name;
        self.lab_harmful_consequences_text.text = data.data.harmful_consequences_text;
        self.lab_rp_risk_content.text = data.data.rp_risk_content;
        self.lab_artificial_control.text = data.data.artificial_control;
        self.lab_management_control.text = data.data.management_control;
        self.lab_technology_control.text = data.data.technology_control;
        self.lab_training_measures.text = data.data.training_measures;
        if ([data.data.rp_lec_level isEqualToString:@"1"]) {
            self.lab_lec_level.text = @"低风险";
        }else if ([data.data.rp_lec_level isEqualToString:@"2"]) {
            self.lab_lec_level.text = @"一般风险";
        }else if ([data.data.rp_lec_level isEqualToString:@"3"]) {
            self.lab_lec_level.text = @"较大风险";
        }else if ([data.data.rp_lec_level isEqualToString:@"4"]) {
            self.lab_lec_level.text = @"重大风险";
        }
        
        self.lab_risk_factor.text = data.data.risk_factor;
        self.lab_control_level_text.text = data.data.control_level_text;
        self.lab_control_department.text = data.data.control_department;
        self.lab_responsible_post.text = data.data.responsible_post;
        
        [AppDelegate addSubview:self.contentView];
    }];
    /**SQRiskPointModel*/
}


- (IBAction)clickContentViewDisappear:(UIButton *)sender {
    [self.contentView removeFromSuperview];
}


- (NSArray<NSArray<NSString *> *> *)classArr{
    return @[@[@"SQCodeTableViewCell"]];
}

-(BaseModel *)modelForHeaderFooter:(NSInteger)section withType:(LCBaseTableVCSectionType)type{
    return  self.codeModel.data[section];
}

-(NSArray<NSString *> *)viewClassForSectionFooterHeader{
    return @[@"SQCodeView"];
}


- (IBAction)clickPicOne:(id)sender {
    [self.view endEditing:YES];
    
    if ([self haveStr:@[self.hd_report_pic_one]]) {
        [SQShowImage showImage:self.img_picOne];
        
    }else{
        [LCImageChoose shareImageWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_picOne.image = data[0];
            self.hd_report_pic_one =   [UIImageJPEGRepresentation(self.img_picOne.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
          
            self.img_picOneDel.image = [UIImage imageNamed:@"del"];
        }];
    }
    
   
}

- (IBAction)clickDelPicOne:(id)sender {
    self.img_picOne.image = [UIImage imageNamed:@"addImage"];
   self.img_picOneDel.image = [UIImage imageNamed:@""];
    self.hd_report_pic_one = @"";
}


- (IBAction)clickPicTwo:(id)sender {
    [self.view endEditing:YES];
    
    if ([self haveStr:@[self.hd_report_pic_two]]) {
        [SQShowImage showImage:self.img_picTwo];
        
    }else{
        [LCImageChoose shareImageWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_picTwo.image = data[0];
            self.hd_report_pic_two = [UIImageJPEGRepresentation(self.img_picTwo.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            
            self.img_picTwoDel.image = [UIImage imageNamed:@"del"];
        }];
    }
    
}

- (IBAction)clickDelPicTwo:(id)sender {
    self.img_picTwoDel.image = [UIImage imageNamed:@""];
    self.img_picTwo.image = [UIImage imageNamed:@"addImage"];
    self.hd_report_pic_two = @"";
   
}


- (IBAction)clickPicThree:(id)sender {
    [self.view endEditing:YES];
    if ([self haveStr:@[self.hd_report_pic_three]]) {
        [SQShowImage showImage:self.img_picThree];
        
    }else{
        [LCImageChoose shareImageWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_picThree.image = data[0];
            self.hd_report_pic_three = [UIImageJPEGRepresentation(self.img_picThree.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            self.img_picThreeDel.image = [UIImage imageNamed:@"del"];
        }];
    }
   
}

- (IBAction)clickDelPicThree:(id)sender {
    self.img_picThreeDel.image = [UIImage imageNamed:@""];
   self.img_picThree.image = [UIImage imageNamed:@"addImage"];
    self.hd_report_pic_three = @"";
}


/**确认正常*/
- (IBAction)clickNormal:(UIButton *)sender {
    
    /**：方法用途：添加扫码及作业检查记录。传参：
     作业任务id(sal_sa),
     风险类型(ir_risktype),
     风险点或作业环节id(ir_ipm_id),
     风险点或作业环节中文(ir_ipm_name),
     检查人员(ir_person),
     检查人员中文(ir_person_text),
     0扫码巡检 1作业检查(ir_mode),
     巡检照片(ir_pic_one),
     巡检照片(ir_pic_two),
     巡检照片(ir_pic_three),
     0无隐患 1有隐患(ir_hidden_trouble),
     企业id(ir_enterprise),
     企业名称(ir_enterprise_text)。
     成功回参：code：200。失败回参：code：0|msg。*/
    
    if (![self haveStr:@[self.hd_report_pic_one,self.hd_report_pic_two,self.hd_report_pic_three]]) {
        
        [CCToast showMessage:@"请上传照片"];
        return;
        
    }
    
//    for (SQCodeModelData * dataModel in self.codeModel.data) {
//        for (SQCodeModelRiskPoint * riskPoint in dataModel.risk_point) {
//            if ([riskPoint.rp_count isEqualToString:@"1"]) {
//                self.ir_hidden_trouble = @"1";
//
//            }else{
//                self.ir_hidden_trouble = @"0";
//            }
//
//        }
//    }
    
    [SQExaminationRecordModel getDataWithParams:@{@"sal_sa":self.sal_sa?self.sal_sa:@"0",
                                                  @"ir_risktype":self.ir_risktype,
                                                  @"ir_ipm_id":self.ipm_id,
                                                  @"ir_ipm_name":self.tf_name.text,
                                                  @"ir_person":UID,
                                                  @"ir_person_text":MName,
                                                  @"ir_mode":self.ir_mode,
                                                  @"ir_pic_one":self.hd_report_pic_one?self.hd_report_pic_one:@"",
                                                   @"ir_pic_two":self.hd_report_pic_two?self.hd_report_pic_two:@"",
                                                   @"ir_pic_three":self.hd_report_pic_three?self.hd_report_pic_three:@"",
                                                  @"ir_hidden_trouble":self.ir_hidden_trouble,
                                                  @"ir_enterprise":MEnterpriseId,
                                                  @"ir_enterprise_text":MEnterpriseText,

    } WithCallBack:^(SQExaminationRecordModel * data) {
        [ CCToast showMessage:@"提交成功"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"codeView" object:nil];
        [self.navigationController popViewControllerAnimated:YES];
        
    }];
    
    
}


- (BOOL)haveStr:(NSArray<NSString *> *)strs{
    for (NSString *key in strs) {
        if (key.length > 0) {
            return YES;
        }
    }
    return NO;
}
  


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
